<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Product extends MY_Controller {
	private $list_cat;
	public function __construct(){
		parent::__construct();
		$this->list_cat = $this->cm->get_all($this->tbl_cat,array('type'=>'product'),'arr');
		$this->load->model('product_model','product');		
		$this->_data['nav_product'] = ' c ';
		$this->load->library('pagination');
	}
		
	public function index(){
		//判断是不是首页
		$this->c_data['per_page'] = !empty($this->c_data['per_page']) ? intval($this->c_data['per_page']) : 0;
		//获取类别选择框中的数据
		$this->_data['arr_cat_tree']= cat_select_tree($this->list_cat);
		//根据搜索及分页条件获得的产品数据
		$this->_data['list_product'] = $this->product->get_by_page($this->c_data);		
		
		//获取分页配置
		$config = $this->config->item('pagination');
		//使用产品模型里的属性
		$config['total_rows'] = $this->product->total_rows;		 //完善分页配置
		//调用总控制器里的获取分页URL方法
		$config['base_url'] = $this->page_url();				 //完善分页配置
		//分页类初始化并创建分页
		$this->pagination->initialize($config);
		$this->_data['pagination'] = $this->pagination->create_links();

		//给侧边子菜单  产品列表  添加样式。
		$this->_data['menu_list'] = ' cm ';
		$this->load->view('product/index',$this->_data);
		
	}	
	
	function cat(){

		//导航名称
		$list_rs = getCatTree($this->list_cat,0);
		$this->_data['list_cat'] = $list_rs;
		
		//current menu
		$this->_data['menu_cat'] = ' cm ';
		$this->load->view('product/cat',$this->_data);
	}
	
	function cat_add(){
		
		echo $this->load->view('product/ajax_cat_add',$this->_data,true);
	}
	
	function cat_sub_add(){
		if(empty($this->c_data['id'])) die;
		$this->_data['pid'] = $this->c_data['id'];
		
		echo $this->load->view('product/ajax_cat_add',$this->_data,true);
	}
	
	function cat_edit(){
		
		if(empty($this->c_data['id'])) die;
		$cat = $this->cm->get_by_id($this->tbl_cat,$this->c_data['id']);
		$this->_data['cat'] = $cat;
		$this->_data['pid'] = $cat->pid;
		
		echo $this->load->view('product/ajax_cat_add',$this->_data,true);
		
	}
	
	function cat_del(){
		if(empty($this->c_data['id'])) die;
		if(hasCatSub($this->list_cat, $this->c_data['id'])) ejd('','只能删除叶子类别！');
		$this->cm->delete($this->tbl_cat,array('id'=>$this->c_data['id']));
		ejd('','','reload');
	}
	
	function del(){
		if(empty($this->c_data['id'])) die;
		$this->product->del_img_by_product($this->c_data['id
				']);
		$this->cm->delete($this->tbl_product,array('id'=>$this->c_data['id']));
		ejd('','','reload');
	}
	
	function cat_add_do(){
		
		if(!$this->form_validation->required($this->c_data['title'])) die;
		$this->c_data['type'] = 'product';
		$flag = $this->cm->save($this->tbl_cat,$this->c_data);
		
		if($flag){
			ejd('','','reload');
		}else{
			ejd('fail','失败！');
		}
		
	}
	
	function add(){
		//取得类别选择框中的数据。
		$this->_data['cat_select_tree'] = cat_select_tree($this->list_cat);
	
		//current menu 选择性添加cm样式。
		$this->_data['menu_list'] = ' cm ';
	
		$this->load->view('product/add',$this->_data);
	
	}
	
	function add_do(){
		//用PHP方式对数据再次验证，防恶意攻击。使用ci框架的 form_validation类 进行服务器端数据验证
		if(!$this->form_validation->required($this->c_data['title'])) die;
		if(!$this->form_validation->required($this->c_data['cid'])) die;
		if(!$this->form_validation->required($this->c_data['title_img_id'])) die;
		if(!$this->form_validation->required($this->c_data['content'])) die;
		if(!$this->form_validation->required($this->c_data['price'])) die;		
		$this->c_data['price'] = floatval($this->c_data['price']);
		//保存产品，含标题图片
		$id = $this->cm->save($this->tbl_product,$this->c_data);
		//保存相册
		if(!empty($this->c_data['album']) && is_array($this->c_data['album'])){
			$album = $this->c_data['album'];
			$desc = $this->c_data['dsrp'];
			$ord = $this->c_data['ord'];
			$this->product->save_album($id,$album,$desc,$ord);
		}
	
		ejd('','','reload');	//因是通过AJAX方式申请的本方法，所以返回一个json格式的数据。供ajax响应，这里就是刷新页面。
	
	}
	
	function del_batch(){
		if(empty($this->c_data['ids'])) die;
		$this->cm->delete_batch($this->tbl_product,$this->c_data['ids']);
		ejd('','','reload');
	}
	
	function edit(){
	
		if(empty($this->c_data['id'])) die;

		$this->_data['cat_select_tree'] = cat_select_tree($this->list_cat);
		
		$product = $this->product->get_by_id($this->c_data['id']);

		$this->_data['product'] = $product;
		
		echo $this->load->view('product/edit',$this->_data,true);
	
	}
		
	function edit_do(){
		//用PHP方式对数据再次验证，防恶意攻击。使用ci框架的 form_validation类 进行服务器端数据验证
		if(!$this->form_validation->required($this->c_data['id'])) die;		
		if(!$this->form_validation->required($this->c_data['title'])) die;
		if(!$this->form_validation->required($this->c_data['cid'])) die;
		if(!$this->form_validation->required($this->c_data['title_img_id'])) die;
		if(!$this->form_validation->required($this->c_data['content'])) die;
		if(!$this->form_validation->required($this->c_data['price'])) die;		
		$this->c_data['price'] = floatval($this->c_data['price']);
		//保存产品，含标题图片
		$id = $this->cm->save($this->tbl_product,$this->c_data);
		//清除原相册，的对应关系。
		$this->cm->delete('product_img',array('pid'=>$id));
		//保存新相册，的对应关系。
		if(!empty($this->c_data['album']) && is_array($this->c_data['album'])){
			$album = $this->c_data['album'];
			$desc = $this->c_data['dsrp'];
			$ord = $this->c_data['ord'];
			$this->product->save_album($id,$album,$desc,$ord);
		}
	
		ejd('','',site_url('c=product&m=index'));	//因是通过AJAX方式申请的本方法，所以返回一个json格式的数据。供ajax响应，这里就是刷新页面。
	}
	
	function clear_img(){
		$this->product->clear_img();
		ejd('','','reload');
	}
	
}
